package io.aicn.lowcode.controller;

import com.fasterxml.jackson.databind.JsonNode;
import io.aicn.lowcode.generator.model.TableInfo;
import io.aicn.lowcode.service.TableMetadataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 表元数据控制器
 * 提供表元数据相关的API接口
 */
@RestController
@RequestMapping("/api/table-metadata")
@Tag(name = "表元数据管理", description = "表元数据相关操作")
public class TableMetadataController {

    private static final Logger log = LoggerFactory.getLogger(TableMetadataController.class);

    @Autowired
    private TableMetadataService tableMetadataService;

    /**
     * 获取表元数据配置
     * @return 表元数据配置JSON
     */
    @GetMapping("/config")
    @Operation(summary = "获取表元数据配置", description = "获取全局字段、领域字段等元数据配置信息")
    public ResponseEntity<JsonNode> getMetadataConfig() {
        try {
            JsonNode config = tableMetadataService.getMetadataConfig();
            return ResponseEntity.ok(config);
        } catch (Exception e) {
            log.error("获取表元数据配置失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }

    /**
     * 获取表元数据Schema
     * @return 表元数据Schema JSON
     */
    @GetMapping("/schema")
    @Operation(summary = "获取表元数据Schema", description = "获取表元数据结构的JSON Schema定义")
    public ResponseEntity<JsonNode> getMetadataSchema() {
        try {
            JsonNode schema = tableMetadataService.getMetadataSchema();
            return ResponseEntity.ok(schema);
        } catch (Exception e) {
            log.error("获取表元数据Schema失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }

    /**
     * 解析表定义
     * @param tableName 表名
     * @return 解析后的表信息
     */
    @GetMapping("/parse/{tableName}")
    @Operation(summary = "解析表定义", description = "根据表名解析JSON格式的表定义，返回TableInfo对象")
    public ResponseEntity<TableInfo> parseTableDefinition(
            @Parameter(description = "表名", required = true)
            @PathVariable String tableName) {
        try {
            TableInfo tableInfo = tableMetadataService.parseTableDefinitionByName(tableName);
            return ResponseEntity.ok(tableInfo);
        } catch (Exception e) {
            log.error("解析表定义失败: {}", tableName, e);
            return ResponseEntity.internalServerError().build();
        }
    }

    /**
     * 解析指定路径的表定义文件
     * @param filePath 文件路径
     * @return 解析后的表信息
     */
    @GetMapping("/parse")
    @Operation(summary = "解析表定义文件", description = "根据文件路径解析JSON格式的表定义文件")
    public ResponseEntity<TableInfo> parseTableDefinitionFile(
            @Parameter(description = "表定义文件路径", required = true)
            @RequestParam String filePath) {
        try {
            TableInfo tableInfo = tableMetadataService.parseTableDefinition(filePath);
            return ResponseEntity.ok(tableInfo);
        } catch (Exception e) {
            log.error("解析表定义文件失败: {}", filePath, e);
            return ResponseEntity.internalServerError().build();
        }
    }

    /**
     * 初始化元数据配置
     * @return 操作结果
     */
    @PostMapping("/initialize")
    @Operation(summary = "初始化元数据配置", description = "重新加载表元数据配置和Schema")
    public ResponseEntity<String> initializeMetadata() {
        try {
            tableMetadataService.initializeMetadata();
            return ResponseEntity.ok("元数据配置初始化成功");
        } catch (Exception e) {
            log.error("初始化元数据配置失败", e);
            return ResponseEntity.internalServerError().body("初始化失败: " + e.getMessage());
        }
    }
}